# -*- coding: utf-8 -*-
# @Time: 2021/8/16 9:34
# @File: problem-24.py
# @DESC: https://leetcode-cn.com/problems/swap-nodes-in-pairs/
# 两两交换链表中的节点，每次交换后赋值给移动指针
from list_node_common import *
class Solution:
    def swapPairs(self, head: ListNode) -> ListNode:
        if head == None or head.next == None:
            return head
        # 奇数
        odd = head
        # 偶数
        even = head.next
        nhead = ListNode(0)
        p = nhead
        while True:
            # 此时已经完成了1和2的交换
            temp = even.next
            even.next = odd
            odd.next = temp

            # 此时偶数再前，将其赋值给指针
            p.next = even
            p = p.next.next

            if odd.next == None or odd.next.next == None:
                break
            else:
                even = odd.next.next
                odd = odd.next
        return nhead.next


solution = Solution()
result = solution.swapPairs(listToNode([1, 2]))
